<template>
  <div>
    <el-form ref="wyform" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="角色名称" prop="name">
        <el-input v-model="form.name" placeholder="请输入角色名称"></el-input>
      </el-form-item>
      
       <el-form-item>
         <el-button type="success" size="small" plain @click="save">保存</el-button>
       </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { post, get ,asyncRequest} from '@/plugins/axios'
export default {
  name: 'RoleEdit',
  props: ['id', 'module'],
  created () {
    if (this.id) {
      this.rules.file = []
      this.getone()
    }
  },
  data () {
	const checkData = async (rule, value, callback) => {
	  const response = await asyncRequest(this.url.check, 'get', { value: value, id: this.id })
	  if (response.data.obj) {
	    callback()
	  } else {
	    callback(new Error('已经存在相同数据'))
	  }
	}
    return {
      url: {
        add: this.module + '/add',
        getone: this.module + '/getone',
        update: this.module + '/update',
		check: this.module + '/check'
      },
      fileList: [],
      form: {
        name: '',
        phone: '',
        email: '',
        icon: '',
        rawPassword: '',
        file: null
      },
      rules: {
        name: [
          { required: true, message: '请输入角色名', trigger: 'blur' },
          { max: 20, message: '用户名最多20个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    save () {
      this.$refs.wyform.validate(valid => {
        if (valid) {
          post(this.id ? this.url.update : this.url.add, this.form, response => {
            this.$emit('update:show', false)
            this.$emit('getTableData')
          })
        }
      })
    },
    getone () {
      get(this.url.getone, { id: this.id }, response => {
        for (const key in this.form) {
          this.form[key] = response[key]
        }
        this.fileList.push({
          name: response.icon,
          url: this.img(response.icon)
        })
        this.form.id = response.id
      })
    },
	changeIcon(){
		
	}
  }
}
</script>

<style scoped lang="less">
</style>
